package com.breaking.second.activity.pager.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.alibaba.fastjson2.JSON;
import com.breaking.second.activity.NewsDetailActivity;
import com.breaking.second.databinding.ItemNewsBinding;
import com.breaking.second.entity.NewsEntity;

import java.util.List;

public class NewsAdapter extends BaseAdapter<ItemNewsBinding> {
    private final List<NewsEntity> newsEntities;

    public NewsAdapter(Context context, List<NewsEntity> newsEntities) {
        super(context);
        this.newsEntities = newsEntities;
    }


    @Override
    public void onBindViewHolder(ItemNewsBinding binding, int position) {
        NewsEntity newsEntity = newsEntities.get(position);
        if (newsEntity.getUnReadNum() == 0) {
            binding.unReadNum.setVisibility(View.GONE);
        }
        binding.unReadNum.setText(String.format("%s", newsEntity.getUnReadNum()));
        binding.username.setText(String.format("%s", newsEntity.getUsername()));
        binding.getRoot().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), NewsDetailActivity.class);
                intent.putExtra("data", JSON.toJSONString(newsEntity));
                getContext().startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return newsEntities.size();
    }
}
